package com.vogue.supplymall.entmanagement.domain.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.vogue.supplymall.entmanagement.domain.dao.BaseDao;
import com.vogue.supplymall.entmanagement.domain.dao.LicenceScopeOfBusinessDao;
import com.vogue.supplymall.entmanagement.domain.dto.LicencesobDTO;
import com.vogue.supplymall.entmanagement.domain.dto.LicencetypeDTO;
import org.springframework.stereotype.Service;

import com.vogue.supplymall.entmanagement.domain.model.LicenceScopeOfBusiness;
import com.vogue.supplymall.entmanagement.domain.service.LicenceScopeOfBusinessService;

/**
 * The type Licence scope of business service.
 */
@Service("licenceScopeOfBusinessService")
public class LicenceScopeOfBusinessServiceImpl 
	extends BaseServiceImpl<LicenceScopeOfBusiness, Long> 
		implements LicenceScopeOfBusinessService{

	@Resource 
	private LicenceScopeOfBusinessDao licenceScopeOfBusinessDao;

	/**
	 * Instantiates a new Licence scope of business service.
	 *
	 * @param baseDao the base dao
	 */
	public LicenceScopeOfBusinessServiceImpl(BaseDao<LicenceScopeOfBusiness, Long> baseDao) {
		super(baseDao);
	}

	@Override
	public List<LicencesobDTO> allScopeOfBusiness() {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = licenceScopeOfBusinessDao.allScopeOfBusiness();
		List<LicencesobDTO> soblist = new ArrayList<>();
		list.parallelStream().forEachOrdered((map) -> {
			
			for(LicencesobDTO sob : soblist) {
				if(sob.getSodid().equals(map.get("sodid").toString())) {
					map.put("isfind", true);
					LicencetypeDTO type = new LicencetypeDTO();
					type.setLicencetypeid(map.get("typeid").toString());
					type.setLicencetypename((String)map.get("typename"));
					List<LicencetypeDTO> typelist = sob.getLicencetypeList();
					typelist.add(type);
					sob.setLicencetypeList(typelist);
					map.put("isfind", true);
					break;
				}
			}
			
			if(map.get("isfind") == null) {
				LicencesobDTO sob = new LicencesobDTO();
				sob.setSodid(map.get("sodid").toString());
				sob.setSodname((String)map.get("sodname"));
				List<LicencetypeDTO> typelist = new ArrayList<>();
				LicencetypeDTO type = new LicencetypeDTO();
				type.setLicencetypeid(map.get("typeid").toString());
				type.setLicencetypename((String)map.get("typename"));
				typelist.add(type);
				sob.setLicencetypeList(typelist);
				soblist.add(sob);
			}

        });
		return soblist;
	}

}
